module executor.message.client.TransferMsgExecutor;

import msgtrans;
import message.Constants;
import message.MsgDefine;
import google.protobuf;
import hunt.logging;
import client.ImClient;
import std.array;
import std.conv : to;
import command.StartAppCommand;
import hunt.util.Serialize;

@TransportClient(msgClientName)
class TransferMsgExecutor : AbstractExecutor!(TransferMsgExecutor){

    @MessageId(MESSAGE.CMD_MESSAGE_TRANSFER)
    void sendMsg(TransportContext ctx, MessageBuffer buffer)
    {
        auto req = new Msg();
        buffer.data.fromProtobuf!Msg(req);
        TransportSession[] clients = ImClient.getInstance().getImClient(to!ulong(req.localId));
        logInfo("UserId: %d, Clients count: %d", req.localId, clients.length);
        logInfo("sendMsg : %d ",toJson(req));
        foreach(TransportSession client ; clients)
        {
            logInfo("to---> : %d ",client.id());
            client.send(new MessageBuffer(MESSAGE.CMD_MESSAGE_TRANSFER,req.toProtobuf.array));
        }
    }



    @MessageId(MESSAGE.CMD_SET_MSG_READ_RES)
    void setMsgReadRes(TransportContext ctx, MessageBuffer buffer)
    {
        auto res = new AlreadyReadREQ();
        buffer.data.fromProtobuf!AlreadyReadREQ(res);
        TransportSession[] clients =    ImClient.getInstance().getImClient(to!ulong(res.clientId));
        logInfo("setMsgReadRes : %d ",toJson(res));
        foreach(TransportSession client ; clients)
        {
            logInfo("to---> : %d ",client.id());
            client.send(new MessageBuffer(MESSAGE.CMD_SET_MSG_READ_RES,res.toProtobuf.array));
        }
    }
}

